﻿using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using LitJson;
using UnityEngine;
using UnityEngine.UI;

public class RegisterPanel : BasePanel
{


	public Button closeBtn;

	public Button sendSmsBtn;

	public Button registerBtn;

	public InputField phoneTextField;

	public InputField validateCodeField;

	public InputField passwordField;
	
	public InputField nickField;

	public Text smsCodeText;
	
	private SendSMSRequest _sendSmsRequest;

	private RegisterRequest _registerRequest;

	private String _rightRandomCode;
	// Use this for initialization
	void Start () {
		//组件初始化
		_sendSmsRequest = this.gameObject.GetComponent<SendSMSRequest>();
		_registerRequest = this.gameObject.GetComponent<RegisterRequest>();
		
		closeBtn.onClick.AddListener(delegate { CloseButtonClicked(); });
		sendSmsBtn.onClick.AddListener(delegate { SendSmsButtonClicked();});
		registerBtn.onClick.AddListener(delegate { RegisterButtonClicked();});
	}
	
	// Update is called once per frame
	void Update () {
		
	}

	/// <summary>
	/// 发送短信验证码
	/// </summary>
	private void SendSmsButtonClicked()
	{
		

			_sendSmsRequest.SendRequest(phoneTextField.text);
		
	}
	
	private void CloseButtonClicked()
	{
		UnityFacade.Instance.UiManager.PopPanel();
	}
	/// <summary>
	/// 执行注册
	/// </summary>
	private void RegisterButtonClicked()
	{
		if (validateCodeField.text.Equals(_rightRandomCode))
		{
		_registerRequest.SendRequest(nickField.text,phoneTextField.text,passwordField.text);
		}
		else
		{
		Debug.Log("短信验证码不正确");
		MsgPanel msgPanel =(MsgPanel) UnityFacade.Instance.UiManager.PushPanel(PanelType.Msg);
		msgPanel.SyncShow("info","短信验证码不正确");
		}
	}
	
	
	public override void OnEnter()
	{
		//先回到初始大小（在进行动画展示）
		this.transform.localScale = Vector3.zero;
		
		if (!this.gameObject.activeSelf)
		{
			this.gameObject.SetActive(true);
		}
		
		
		transform.DOScale(new Vector3(1, 1, 1), 0.5f);
	}

	public override void OnPause()
	{
		
	}

	public override void OnResume()
	{
		
	}

	public override void OnExit()
	{
		transform.DOScale(new Vector3(0, 0, 0), 0.5f).OnComplete(delegate { this.gameObject.SetActive(false); });
	}

	public void HandleResponse(JsonData jsonData)
	{
		string state  = jsonData["state"].ToString();
		Debug.Log(jsonData["msg"]);// 改为提示面板
		
		if (state.Equals("fail"))
		{
			MsgPanel msgPanel =(MsgPanel) UnityFacade.Instance.UiManager.PushPanel(PanelType.Msg);
			msgPanel.SyncShow("fail",jsonData["msg"].ToString());
			
		}else if (state.Equals("success"))
		{
			_rightRandomCode = jsonData["data"].ToString();
			//打开短信验证码的输入和注册按钮
			smsCodeText.gameObject.SetActive(true);
			validateCodeField.gameObject.SetActive(true);
			sendSmsBtn.transform.DOScale(Vector3.zero, 1).OnComplete(delegate
				{
					registerBtn.transform.localScale = Vector3.zero;
					registerBtn.gameObject.SetActive(true);
					registerBtn.transform.DOScale(new Vector3(1, 1, 1), 1);
				});


		}
	}

	public void HandleRegisterResponse(JsonData jsonData)
	{
		Debug.Log(jsonData["msg"].ToString());
		MsgPanel msgPanel =(MsgPanel) UnityFacade.Instance.UiManager.PushPanel(PanelType.Msg);
		msgPanel.SyncShow(jsonData["state"].ToString(),jsonData["msg"].ToString());
	}
}
